﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    Layout = null;
}
<link href="~/lib/layui/css/layui.css" rel="stylesheet" />
<script src="~/lib/layui/layui.js"></script>
<script src="~/js/js.cookie.js"></script>
<script src="~/js/moment.js"></script>
<script src="~/lib/jquery/dist/jquery.js"></script>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    <link href="//unpkg.com/layui@2.9.20/dist/css/layui.css" rel="stylesheet">
</head>
<body>
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">权限名称</label>
            <div class="layui-input-block">
                <input type="text" id="permissionName" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">权限类型</label>
            <div class="layui-input-block">
                <select name="interest" lay-filter="aihao" id="permissionTypeId">
                    <option value="0" selected>请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item" pane>
            <label class="layui-form-label">权限状态</label>
            <div class="layui-input-block"  >
                <input type="radio" name="permissionState" value="1" title="有效">
                <input type="radio" name="permissionState"  value="2" title="无效">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="demo1" onclick="AddPermission()">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</body>
</html>

<script>
    var form = layui.form;
    var $ = layui.jquery;

    $(function () {
        GetPer()  // 权限分类
    })

    //权限添加
    function AddPermission() { 
        let data = {
            PermissionCoding: "",
            PermissionName: $("#permissionName").val(),
            PermissionTypeId: $("#permissionTypeId").val(),
            PermissionState: $("[name=permissionState]:checked").val(),
            IsDelete: $("#isDelete").val() || 0,
            Url: "",
            CreatedOwner: localStorage.getItem('userName'),
            CreatedTime: $("#createdTime").val() || new Date().toISOString(), // 如果不存在，则默认为当前时间（ISO格式）,
            UpdateOwner: "",
            UpdateTime: new Date().toISOString(),
            ExamineOwner: "",
            ExamineTime: new Date().toISOString(),
        }
        // console.log(typeof(data))
        $.ajax({
            url: '@ViewBag.RBACWrite' + "/api/Permissions/AddCondition",
            type:"post",
            data: JSON.stringify(data),
            dataType: "json",
            contentType: "application/json",
            success: function(res){
                if (res.code == 0) {
                    alert("权限添加成功")
                    location.href = '/Permission/QueryPermission'
                } else {
                    alert("权限添加失败")
                }
            }
        })
        event.preventDefault(); // 阻止表单默认提交
    }

    // 权限分类列表
    function GetPer() {
        $.ajax({
            url: '@ViewBag.RBACWrite' + "/api/Permissions/GetPermissionTypes",
            data: JSON.stringify({}),
            type: "post",
            dataType: "json",
            contentType: "application/json",
            success: res => {
                $.each(res.data, function () {
                    $("#permissionTypeId").append(`<option value="${this.permissionTypeId}">${this.permissionTypeName}</option>`);
                });
                form.render('select'); // 重新渲染下拉框
            },
        });
    }
</script>